package cn.msn.order.service.impl;

import cn.msn.basic.service.impl.BaseServiceImpl;
import cn.msn.basic.util.LoginContext;
import cn.msn.basic.util.PageList;
import cn.msn.order.domain.OrderPetAcquisition;
import cn.msn.order.dto.OrderDto;
import cn.msn.order.mapper.OrderPetAcquisitionMapper;
import cn.msn.order.query.OrderPetAcquisitionQuery;
import cn.msn.order.service.IOrderPetAcquisitionService;
import cn.msn.org.domain.Employee;
import cn.msn.org.domain.Shop;
import cn.msn.org.mapper.EmployeeMapper;
import cn.msn.org.mapper.ShopMapper;
import cn.msn.pet.domain.Pet;
import cn.msn.pet.domain.SearchMasterMsg;
import cn.msn.pet.mapper.PetMapper;
import cn.msn.pet.mapper.SearchMasterMsgMapper;
import cn.msn.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
@RocketMQMessageListener(topic = "order_topic",consumerGroup = "consumer_order_tag")
@Slf4j
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService, RocketMQListener<OrderPetAcquisition> {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断登录员工的角色
        Long shopId = employee.getShop_id();
        // 2.店铺人员
        if (shopId!=null) {
            Shop shop = shopMapper.loadById(shopId);
            if (!shop.getAdminId().equals(employee.getId())) {
                // 3.店员
                query.setEmpId(employee.getId());
            }
            // 4.店铺管理员(employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id)
            query.setShopId(shopId);
        }
        // 5.平台管理员=（employee.shop_id==null）
        return super.queryPage(query);

    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        // 确认订单需要修改订单的状态、支付方式、具体金额
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        // 1 已处理
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);

        // 修改寻主消息状态 1待处理   2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        // 修改宠物的成本价
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void cancel(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        // 1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        // 2.删除宠物信息
        petMapper.remove(order.getPetId());

        // 3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    public void onMessage(OrderPetAcquisition orderPetAcquisition) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderPetAcquisition.getId());
        log.info("延迟消息"+order);
        if (order.getState()==0){
            // 放到寻主池
            searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());
            // 删除宠物信息
            petMapper.remove(orderPetAcquisition.getId());
            // 删除dindan
            orderPetAcquisitionMapper.remove(orderPetAcquisition.getId());

        }

    }
}
